﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using OpenLab.Model;

namespace OpenLab.DAL
{
	public partial class ClientDAL : OpenLab.DAL.IClientDAL
	{
        public Client Add
			(Client client)
		{
				string sql ="INSERT INTO Client (nom, prenom, datenaissance, sexe, tel, idorganisme, coderecherche)  output inserted.id VALUES (@nom, @prenom, @datenaissance, @sexe, @tel, @idorganisme, @coderecherche)";
				SqlParameter[] para = new SqlParameter[]
					{
						new SqlParameter("@nom", ToDBValue(client.nom)),
						new SqlParameter("@prenom", ToDBValue(client.prenom)),
						new SqlParameter("@datenaissance", ToDBValue(client.datenaissance)),
						new SqlParameter("@sexe", ToDBValue(client.sexe)),
						new SqlParameter("@tel", ToDBValue(client.tel)),
						new SqlParameter("@idorganisme", ToDBValue(client.idorganisme)),
						new SqlParameter("@coderecherche", ToDBValue(client.coderecherche)),
					};
					
				int newId = (int)SqlHelper.ExecuteScalar(sql, para);
				return GetById(newId);
		}

        public int DeleteById(int id)
		{
            string sql = "DELETE Client WHERE Id = @Id";

           SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};
		
            return SqlHelper.ExecuteNonQuery(sql, para);
		}
		
				
        public int Update(Client client)
        {
            string sql =
                "UPDATE Client " +
                "SET " +
			" nom = @nom" 
                +", prenom = @prenom" 
                +", datenaissance = @datenaissance" 
                +", sexe = @sexe" 
                +", tel = @tel" 
                +", idorganisme = @idorganisme" 
                +", coderecherche = @coderecherche" 
               
            +" WHERE id = @id";


			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@id", client.id)
					,new SqlParameter("@nom", ToDBValue(client.nom))
					,new SqlParameter("@prenom", ToDBValue(client.prenom))
					,new SqlParameter("@datenaissance", ToDBValue(client.datenaissance))
					,new SqlParameter("@sexe", ToDBValue(client.sexe))
					,new SqlParameter("@tel", ToDBValue(client.tel))
					,new SqlParameter("@idorganisme", ToDBValue(client.idorganisme))
					,new SqlParameter("@coderecherche", ToDBValue(client.coderecherche))
			};

			return SqlHelper.ExecuteNonQuery(sql, para);
        }		
		
        public Client GetById(int id)
        {
            string sql = "SELECT * FROM Client WHERE Id = @Id";
            using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql, new SqlParameter("@Id", id)))
			{
				if (reader.Read())
				{
					return ToModel(reader);
				}
				else
				{
					return null;
				}
       		}
        }
		
		public Client ToModel(SqlDataReader reader)
		{
			Client client = new Client();

			client.id = (int)ToModelValue(reader,"id");
			client.nom = (string)ToModelValue(reader,"nom");
			client.prenom = (string)ToModelValue(reader,"prenom");
			client.datenaissance = (DateTime)ToModelValue(reader,"datenaissance");
			client.sexe = (int)ToModelValue(reader,"sexe");
			client.tel = (string)ToModelValue(reader,"tel");
			client.idorganisme = (int?)ToModelValue(reader,"idorganisme");
			client.coderecherche = (string)ToModelValue(reader,"coderecherche");
			return client;
		}
		
		public int GetTotalCount()
		{
			string sql = "SELECT count(*) FROM Client";
			return (int)SqlHelper.ExecuteScalar(sql);
		}
		
		public IEnumerable<Client> GetPagedData(int minrownum,int maxrownum)
		{
			string sql = "SELECT * from(SELECT *,row_number() over(order by id) rownum FROM Client) t where rownum>=@minrownum and rownum<=@maxrownum";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql,
				new SqlParameter("@minrownum",minrownum),
				new SqlParameter("@maxrownum",maxrownum)))
			{
				return ToModels(reader);					
			}
		}
		
		public IEnumerable<Client> GetAll()
		{
			string sql = "SELECT * FROM Client";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql))
			{
				return ToModels(reader);			
			}
		}
		
		protected IEnumerable<Client> ToModels(SqlDataReader reader)
		{
			var list = new List<Client>();
			while(reader.Read())
			{
				list.Add(ToModel(reader));
			}	
			return list;
		}		
		
		protected object ToDBValue(object value)
		{
			if(value==null)
			{
				return DBNull.Value;
			}
			else
			{
				return value;
			}
		}
		
		protected object ToModelValue(SqlDataReader reader,string columnName)
		{
			if(reader.IsDBNull(reader.GetOrdinal(columnName)))
			{
				return null;
			}
			else
			{
				return reader[columnName];
			}
		}
	}
}
